using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;
/// <summary>
/// Summary description for AccountDB
/// </summary>
/// 
namespace CE.User
{
    public class AccountDB
    {
        public AccountDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }
              
        public Int64 Insert(Account account)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCXTHReportsUserInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@UserId", account.UserId);
            _sPara.Add("@UserRole", account.UserRole);
            _sPara.Add("@Email", account.Email);
            _sPara.Add("@AuthCode", account.AuthCode);
            _sPara.Add("@UserTypeId", account.UserTypeId);
            _sPara.Add("@RefUserId", account.RefUserId);
            _sPara.Add("@Status", account.Status);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public Int64 UpdatePassword(Int64 UserId, string AuthCode)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCXTHReportsUserPasswordUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@UserId", UserId);
            _sPara.Add("@AuthCode", AuthCode);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public bool IsAlreadyExist(string Email, int UserTypeId)
        {
            bool _result = false;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXTHUserSelectSingleItemByEmail]";

            SortedList _spara = new SortedList();
            _spara.Add("@Email", Email);
            _spara.Add("@Status", true);
            _spara.Add("@UserTypeId", UserTypeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _result = true;
                }
            }
            return _result;
        }

        public Account CheckEmail(string Email, int UserTypeId)
        {
            Account _account = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXTHUserSelectSingleItemByEmail]";

            SortedList _spara = new SortedList();
            _spara.Add("@Email", Email);
            _spara.Add("@Status", true);
            _spara.Add("@UserTypeId", UserTypeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _account = new Account();
                    _account = FillDataRecord(_dt);
                }
            }
            return _account;
        }

        private Account FillDataRecord(DataTable myDataTable)
        {
            Account myAccount = new Account();
            myAccount.UserId = Convert.ToInt64(myDataTable.Rows[0]["UserId"].ToString());
            myAccount.UserRole = Convert.ToInt32(myDataTable.Rows[0]["UserRole"].ToString());
            myAccount.Email = myDataTable.Rows[0]["Email"].ToString();
            myAccount.AuthCode = myDataTable.Rows[0]["AuthCode"].ToString();
            myAccount.UserTypeId = Convert.ToInt32(myDataTable.Rows[0]["UserTypeId"].ToString());
            myAccount.RefUserId = Convert.ToInt64(myDataTable.Rows[0]["RefUserId"].ToString());
            myAccount.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            return myAccount;
        }

    }
}